package cn.changein.framework.modular.db.factory;

import cn.changein.framework.config.properties.DruidProperties;
import cn.changein.framework.modular.db.model.param.DatabaseInfoParam;

/**
 * 数据库信息工厂
 * 

 * @date: 2020-02-17
 * @copyright: 广州澄境科技有限公司
 */
public class DataBaseInfoFactory {

    /**
     * 数据库信息工厂
     * @param druidProperties
     * @param databaseName
     * @return
     */
    public static DatabaseInfoParam createDataBaseInfo(DruidProperties druidProperties, String databaseName) {
        DatabaseInfoParam databaseInfo = new DatabaseInfoParam();

        databaseInfo.setDbName(databaseName);
        databaseInfo.setJdbcDriver(druidProperties.getDriverClassName());
        databaseInfo.setUserName(druidProperties.getUsername());
        databaseInfo.setPassword(druidProperties.getPassword());

        //根据旧的url，拼接新的url
        String jdbcUrl = druidProperties.getUrl();
        int first = jdbcUrl.lastIndexOf("/") + 1;
        int last = jdbcUrl.indexOf("?");

        //替换旧的名称
        String newUrl = jdbcUrl.substring(0, first) + databaseName + jdbcUrl.substring(last);
        databaseInfo.setJdbcUrl(newUrl);

        return databaseInfo;
    }

}

